package com.controller;


import com.domain.Grade;
import com.domain.UG;
import com.domain.User;
import com.service.IGradeService;
import com.service.IUserService;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: wu linchun
 * @time: 2021/4/4 22:50
 */
@Controller
@RequestMapping("/myc")
public class MyController {

    IUserService iUserService;
    IGradeService iGradeService;

    public MyController() {
    }

    @Autowired
    public MyController(IUserService iUserService, IGradeService iGradeService) {
        this.iUserService = iUserService;
        this.iGradeService = iGradeService;
    }

    @RequestMapping(value = "/index")
    public String index() {
        System.out.println("首页");
        return "/html/index";
    }

    @RequestMapping(value = "/tologin")
    public String tologin() {
        System.out.println("登录界面");
       // System.out.println(iUserService.queryUserbyUserName("root").toString());
        return "/html/tologin";
    }

    @RequestMapping(value = "/login")
    //@ResponseBody
    public String login(String username, String password, Model model) {
        System.out.println("xxxxxxxxx");
        System.out.print(username+" \\"+password);
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        System.out.println("token="+token.toString());
        try {
            subject.login(token);
            return "/html/index";
        } catch (UnknownAccountException uae) {
            model.addAttribute("msg", "用户名不存在");
            return "/html/tologin";
        } catch (IncorrectCredentialsException ice) {
            model.addAttribute("msg", "密码不正确");
            return "/html/tologin";
        } catch (LockedAccountException lae) {
            model.addAttribute("msg", "账户被锁");
            return "/html/tologin";
        }
    }

    @RequestMapping(value = "/gradeM")
    public String showGrade(Model model) {
        List<UG> ugList = new ArrayList<UG>();
        ugList = iGradeService.allGrade();
        model.addAttribute("ug_model", ugList);
        return "/html/user/gradeM";
    }

    @RequestMapping(value = "/showuser")
    public String showuser(Model model) {
        List<User> userList = new ArrayList<User>();
        userList = iUserService.allUser();
        model.addAttribute("user_model", userList);
        return "/html/user/userM";
    }

    @RequestMapping(value = "/myGrade")
    public String myGrade(int id,Model model) {
        List<Grade> gradeList = new ArrayList<Grade>();
        gradeList = iGradeService.queryGradebyId(id);
        model.addAttribute("myGrade_model",gradeList);
        return "/html/user/myGrade";
    }


    @RequestMapping(value = "/unauthorized")
    @ResponseBody
    public String unauthorized() {
        return "未经授权！！！";
    }

    @RequestMapping(value = "/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        session.removeAttribute("user_session");
        subject.logout();
        return "redirect:/myc/index";
    }

}
